package com.guoguo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoguo.base.PageBean;
import com.guoguo.base.PaginationContext;
import com.guoguo.entity.Model;
import com.guoguo.entity.vo.AttentionVO;
import com.guoguo.entity.vo.ImagesVO;
import com.guoguo.entity.vo.ModelAttentionVO;
import com.guoguo.entity.vo.ModelsVO;
import com.guoguo.enums.AliOssImageEnum;
import com.guoguo.mapper.AttentionMapper;
import com.guoguo.mapper.ImagesMapper;
import com.guoguo.service.ModelsService;
import com.guoguo.mapper.ModelMapper;
import com.guoguo.utils.AliOssUtils;
import com.guoguo.utils.aliOss.AliOssImageConfig;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/16.
 */
@Service
public class ModelsServiceImpl implements ModelsService {


    //modelMapper接口
    @Autowired
    private ModelMapper modelsMapper;

    @Autowired
    private AttentionMapper attentionMapper;

    @Autowired
    private ImagesMapper imagesMapper;

    @Override
    public Model getModelsById(String modelId) {
        return modelsMapper.getModelsById(modelId);
    }

    @Override
    public int getModels(String ModelId) {
        return modelsMapper.getModels(ModelId);
    }


    @Override
    public PageBean<ModelsVO> getModelList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ModelsVO> modelsVOs = modelsMapper.getModelList(pageNum);
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
        for (ModelsVO modelsVO : modelsVOs) {
            String icon = AliOssUtils.getImageUrl(modelsVO.getModelIcon());//, aliOssImageConfig);
            modelsVO.setModelIcon(icon);
        }

        return new PageBean<ModelsVO>(modelsVOs);

    }


    @Override
    public PageBean<AttentionVO> getAttentionModelList(String userId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AttentionVO> attentionVOs = attentionMapper.getAttentionModelList(userId, pageNum, pageSize);
//        List<AliOssImageConfig> aliOssImageConfigsList = new ArrayList<>();
//        aliOssImageConfigsList.add(new AliOssImageConfig("test"));
        for (AttentionVO attentionVO : attentionVOs) {
            attentionVO.setModelIcon(AliOssUtils.getImageUrl(attentionVO.getModelIcon()));//, new AliOssImageConfig("test")));
            List<ImagesVO> imagesVOs = imagesMapper.getAlbumImageVOList(attentionVO.getAlbId());//attentionVO.getImagesVOs();
            for (ImagesVO imagesVO : imagesVOs) {
                List<String> files = new ArrayList<>();
                files.add(imagesVO.getOriginalUrl());
                files.add(imagesVO.getThumbnailsUrl());
                Map<String, String> urlMap = AliOssUtils.getImageUrlList(files);//, aliOssImageConfigsList);
                imagesVO.setOriginalUrl(urlMap.get(imagesVO.getOriginalUrl()));
                imagesVO.setThumbnailsUrl(urlMap.get(imagesVO.getThumbnailsUrl()));
            }
            attentionVO.setImagesVOs(imagesVOs);
        }
        return new PageBean<AttentionVO>(attentionVOs);
    }

    @Override
    public int deleteByPrimaryKey(String modelId) {
        return modelsMapper.deleteByPrimaryKey(modelId);
    }

    @Override
    public Model selectByPrimaryKey(String modelId) {
        Model model = modelsMapper.selectByPrimaryKey(modelId);
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
        if (model != null) {
            String icon = AliOssUtils.getImageUrl(model.getModelIcon());//, aliOssImageConfig);
            String cover = AliOssUtils.getImageUrl(model.getModelCover());//, aliOssImageConfig);
            model.setModelIcon(icon);
            model.setModelCover(cover);
        }
        return model;
    }

    @Override
    public Model updateBySelective() {
        return modelsMapper.updateBySelective();
    }

    @Override
    public int updateModel(Model model) {
        return modelsMapper.updateModel(model);
    }

    @Override
    public int updateByPrimaryKey(Model model) {
        return modelsMapper.updateByPrimaryKey(model);
    }

    @Override
    public Model updateByPrimaryKeySelective(Model model) {
        int num = modelsMapper.updateByPrimaryKeySelective(model);
        return model;
    }


    @Override
    public int insertSelective(Model model) {
        int num = modelsMapper.insertSelective(model);
        return num;
    }

    @Override
    public int insert(Model model) {
        return modelsMapper.insert(model);
    }

    @Override
    public PageBean<ModelsVO> selectModelsList(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<ModelsVO> modelsVOs = modelsMapper.selectModelsList();
        return new PageBean<ModelsVO>(modelsVOs);
    }


    @Override
    public List<ModelsVO> selectModelList() {

        return modelsMapper.selectModelList();
    }

    @Override
    public int UDByPrimaryKey(String modelId) {
        return modelsMapper.UDByPrimaryKey(modelId);
    }

    @Override
    public Model addModel(String modelId, String modelName, String nickName, String modelIcon, String modelCover, String modelJob, int sex, String height, String weight, String bust, String waistline, String hipline, String country, String city, String args1) {
        //添加修改
        Model model = modelsMapper.selectByPrimaryKey(modelId);
        if (model == null) {
            model = new Model(modelId, modelName, nickName, modelIcon, modelCover, modelJob, sex, height, weight, bust, waistline, hipline, country, city, args1);
            int num = modelsMapper.insertSelective(model);
            return model;
        } else {
            model.setModelName(modelName);
            model.setNickName(nickName);
            model.setModelIcon(modelIcon);
            model.setModelCover(modelCover);
            model.setModelJob(modelJob);
            model.setSex(sex);
            model.setHeight(height);
            model.setWeight(weight);
            model.setBust(bust);
            model.setWaistline(waistline);
            model.setHipline(hipline);
            model.setCountry(country);
            model.setCity(city);
            model.setArgs1(args1);
            int num = modelsMapper.updateByPrimaryKeySelective(model);
            return model;
        }

    }
}
